import os
import ctypeslib.h2xml
import ctypeslib.xml2py


include_dir = os.environ["VXIPNPPATH"]+"WinNT\\include"
source_dir = os.path.dirname(os.path.abspath(__file__))

source_file = os.path.join(include_dir,'niScope.h')
target_file = os.path.join(source_dir,'niScope.xml')
target_py = os.path.join(source_dir,'Types2.py')
error_symbols_file = os.path.join(source_dir,'Errors.py')
def_file = os.path.join(source_dir,'Definitions.py')
attributes_file = os.path.join(source_dir,'attributes.py')
vi_symbols_file = os.path.join(source_dir,'vi_symbols.py')
cmd = ["",'-I"%s"'%include_dir,"-DWIN32",
       source_file,"--cpp-symbols",
       '-o%s'%target_file]
ctypeslib.h2xml.compile_to_xml(cmd)
cmd = ["",target_file,
   #    "-l niScope_32",
       "-kefst",
       "-o",target_py]
ctypeslib.xml2py.main(cmd)
cmd = ["",target_file,
       "-kd",
       "-o",def_file]
ctypeslib.xml2py.main(cmd)

f = open(def_file,'r')
lines = f.readlines()
f.close()
#filter from definitions
attributes_lines = [line for line in lines 
                    if line.startswith("NISCOPE_ATTR")]
error_lines = [line for line in lines 
               if line.startswith("NISCOPE_ERR") or line.startswith("VI_ERROR")]
vi_lines = [line for line in lines 
            if line.startswith("VI_") and not line.startswith("VI_ERROR")]
f = open(attributes_file,'w')
f.write("".join(attributes_lines))
f.close
f = open(error_symbols_file,'w')
f.write("".join(error_lines))
f.close
f = open(vi_symbols_file,'w')
f.write("".join(vi_lines))
f.close